package GraghX.algorithms

import org.apache.spark.graphx.{GraphLoader, PartitionStrategy}
import org.apache.spark.{SparkConf, SparkContext}

object TriangleCountingDemo {
def main(args: Array[String]): Unit = {
//设置运行环境
  val conf  = new SparkConf().setAppName("TriangleCountingDemo").setMaster("local")
  val sc = new SparkContext(conf)
  sc.setLogLevel("WARN")

  //导入数据
  val graph = GraphLoader.edgeListFile(sc,"d:/tmp/followers.txt",true)
    .partitionBy(PartitionStrategy.RandomVertexCut)
  //遍历顶点找三角形个数
  val triCounts = graph.triangleCount().vertices
  triCounts.collect.foreach(println(_))
  println("****************************")
  //利用用户名结合三角形个数
  val users = sc.textFile("d:/tmp/users.txt").map{ line =>
    val fields = line.split(",")
    (fields(0).toLong,fields(1))
  }
  val triCountsByUserName = users.join(triCounts).map{ case (id,(username,tc)) =>
    (username,tc)
  }

    //结果
  println(triCountsByUserName.collect().mkString("\n"))

}
}
